<template>
    <div>
        <van-nav-bar
                title="地址编辑"
                left-arrow
                class="my-nav"
                :fixed="true"
                @click-left="$router.back(-1)"
        />
        <div style="margin-top: 50px;">
            <van-address-edit
                    :address-info="address"
                    :area-list="areaList"
                    show-delete
                    :show-set-default="false"
                    @save="onSave"
                    @delete="onDelete"
            />
        </div>
    </div>
</template>

<script>
    import {Toast} from 'vant'
    import {
        regionList,
        saveShippingAddress,
        updateShippingAddress,
        shippingAddressDetail,
        deleteShippingAddress
    } from '../../api/address'

    export default {
        data() {
            return {
                address: {
                    name: "",
                    tel: "",
                    province: "",
                    city: "",
                    county: "",
                    areaCode: "",
                    postalCode: "",
                    addressDetail: ""
                },
                areaList: {}
            }
        },

        mounted() {
            regionList(d => {
                this.areaList = d
                if (this.$route.query.id) {
                    shippingAddressDetail(this.$route.query.id, d => {
                        let obj = this.address;
                        obj.id = d.id
                        obj.name = d.name
                        obj.tel = d.phone
                        if (d.region) {
                            let region = d.region.split("-")
                            obj.province = region[0]
                            obj.city = region[1]
                            obj.county = region[2]
                            obj.areaCode = this.getAreaCode(obj.county)
                        }
                        obj.addressDetail = d.address
                        obj.isDefault = d.enabled
                    })
                }
            })

        }
        ,
        methods: {
            onSave(content) {
                let name = content.name
                let phone = content.tel
                let region = content.province + "-" + content.city + "-" + content.county
                let address = content.addressDetail
                let enabled = content.isDefault
                let obj = {name, phone, region, address, enabled}
                if (this.$route.query.id) {
                    updateShippingAddress(this.$route.query.id, obj, _ => {
                        Toast('收货地址编辑成功!')
                        setTimeout(() => this.$router.back(-1), 3000);
                    }, _ => Toast('收货地址编辑失败!'))
                } else {
                    saveShippingAddress(obj, _ => {
                        Toast('收货地址保存成功!')
                        setTimeout(() => this.$router.back(-1), 3000);
                    }, _ => Toast('收货地址保存失败!'))
                }

            },
            onDelete() {
                deleteShippingAddress(this.$route.query.id, _ => {
                    Toast('收货地址删除成功!')
                    setTimeout(() => this.$router.back(-1), 3000);
                }, _ => Toast('收货地址删除失败!'))
            },
            getAreaCode(county) {
                for (let key in this.areaList.county_list) {
                    let val = this.areaList.county_list[key];
                    if (val === county) {
                        return key;
                    }
                }
            }
        }
    }
</script>

<style lang="less" scoped>

</style>